﻿using AutoMapper;
using MediatR;
using SupplyChain.Infrastructure.Interface;
using SupplyChain.Write.API.Application.Command.PurchaseOrder;

namespace SupplyChain.Write.API.Application.CommandHandler.PurchaseOrder
{
    public class UpdPurchaseOrderHandler : IRequestHandler<UpdPurchaseOrderCommand, bool>
    {
        private readonly IBaseRepository<Domain.Entities.PurchaseOrder> repository;
        private readonly IMapper mapper;

        public UpdPurchaseOrderHandler(IBaseRepository<Domain.Entities.PurchaseOrder> repository, IMapper mapper)
        {
            this.repository = repository;
            this.mapper = mapper;
        }

        public async Task<bool> Handle(UpdPurchaseOrderCommand request, CancellationToken cancellationToken)
        {
            Domain.Entities.PurchaseOrder order = mapper.Map<Domain.Entities.PurchaseOrder>(request);
            if (order.Id != 0)
            {
                bool result = await repository.UpdateAsync(order);
                return await Task.FromResult(result);
            }
            return await Task.FromResult(false);
        }
    }
}
